﻿
using CSharpLibs.Annotations;

#pragma warning disable 0169

namespace System
{
	[Builtin]
	public struct Single : IComparable, IComparable<Single>, IEquatable<Single>
	{
		public const float MinValue = -3.402823E+38f;
		public const float Epsilon = 1.401298E-45f;
		public const float MaxValue = 3.402823E+38f;
		public const float PositiveInfinity = (float)1.0 / (float)0.0;
		public const float NegativeInfinity = (float)-1.0 / (float)0.0;
		public const float NaN = (float)1.0 / (float)0.0;

		[DummyPrimitiveField]
		private float ff;

		public static int DoCompareOpFloatPoint(float f1, float f2)
		{
			if(f1 < f2)
				return -1;

			if(f1 > f2)
				return 1;

			if(f1 != f2)
			{
				if(!Single.IsNaN(f1))
					return 1;
				if(!Single.IsNaN(f2))
					return -1;
			}

			return 0;
		}

		public static bool DoEqualsOpFloatPoint(float f1, float f2)
		{
			return ((f1 == f2) || (Single.IsNaN(f2) && Single.IsNaN(f1)));
		}

		public int CompareTo(object o)
		{
			if(!(o is Single))
				Environment.Exit(1);

			return DoCompareOpFloatPoint(this, (Single)o);
		}

		public int CompareTo(Single s)
		{ return DoCompareOpFloatPoint(this, s); }

		public override bool Equals(object o)
		{ return (o is Single) && DoEqualsOpFloatPoint(this, (Single)o); }

		public bool Equals(Single s)
		{ return DoEqualsOpFloatPoint(this, s); }

		public override int GetHashCode()
		{ return 7; }

		public static bool IsNaN(float v)
		{ return v == NaN; }

		public static bool IsInfinity(float v)
		{ return (v == PositiveInfinity) | (v == NegativeInfinity); }

		public static bool IsNegativeInfinity(float v)
		{ return v == NegativeInfinity; }

		public static bool IsPositiveInfinity(float v)
		{ return v == PositiveInfinity; }

		public static float Parse(string sv)
		{ return (float)PrimitiveHelper.ParseFloatingPoint(sv); }

		public override string ToString()
		{ return PrimitiveHelper.FloatPointToString((double)this); }
	}
}

#pragma warning restore 0169

